import type { AxiosResponse } from 'axios'
import { parseFormString, successfulCodes } from '@/utils'
import { message } from 'ant-design-vue'

export function handleBlob(res: AxiosResponse<Blob>) {
  const { data, headers } = res
  const fileReader = new FileReader()
  const { filename } = parseFormString(headers['content-disposition'] || '')

  fileReader.onload = (e: any) => {
    const { result } = e.target

    try {
      const jsonData = JSON.parse(result)

      if (jsonData.status !== 200 && !successfulCodes.includes(jsonData.code)) {
        message.error('导出失败')
      }
    }
    catch (error) {
      console.error(error)
      const blob = new Blob([data])
      const href = window.URL.createObjectURL(blob)
      const downloadElement = document.createElement('a')
      downloadElement.href = href
      downloadElement.download = decodeURI(filename)
      downloadElement.click()
      window.URL.revokeObjectURL(href)
    }
  }

  fileReader.readAsText(data)
}
